package com.summer.order.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import com.summer.order.model.ReportOrder;
import com.summer.order.model.UserDayFood;
import com.summer.order.model.UserOrder;
import com.summer.order.service.UserOrderService;

@Service("userOrderService")
public class UserOrderServiceImpl implements UserOrderService {

	@Resource
	HibernateTemplate ht;

	@Override
	public void saveOrder() {
		
	}
	
	@Override
	public List<UserDayFood> getTodayLunchList() {
		List<UserDayFood> list = ht.find("from UserDayFood userDayFood where date_format(userDayFood.date,'%Y%m%d')=? and userDayFood.lunch = 'yes'", new SimpleDateFormat("yyyyMMdd").format(new Date()) );
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}
	
	@Override
	public List<UserDayFood> getTodayBreakfastList() {
		List<UserDayFood> list = ht.find("from UserDayFood userDayFood where date_format(userDayFood.date,'%Y%m%d')=? and userDayFood.breakfast = 'yes'", new SimpleDateFormat("yyyyMMdd").format(new Date()) );
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}
	
	@Override
	public ReportOrder getReportOrder(List<UserOrder> userOrderList) {

		ReportOrder ro = new ReportOrder();
		
		Integer countBreakfast = 0;
		Integer countLunch = 0;
		Double money = 0.0;
		Integer countMember = 0;
		List<UserDayFood> dayFoodList;
		for (UserOrder userOrder : userOrderList) {
			dayFoodList = new ArrayList<UserDayFood>(userOrder.getUserDayFoods());
			countMember++;
			for (UserDayFood userDayFood : dayFoodList) {
				
				if(userDayFood.getBreakfast().equals("yes")) {
					countBreakfast++;
					money += 3;
				}
				if(userDayFood.getLunch().equals("yes")) {
					money += 6;
					countLunch++;
				}
			}
			
		}
		ro.setCountBreakfast(countBreakfast);
		ro.setCountLunch(countLunch);
		ro.setCountMember(countMember);
		ro.setMoney(money);
		return ro;
	
	}

}
